import axios from "axios";
const server = 'http://localhost:8181'

// http request 拦截器
axios.interceptors.request.use(
    config => {
        //debugger
        if (localStorage.token || sessionStorage.token) { //判断token是否存在
            if(localStorage.token){
                config.headers.token = localStorage.token;  //将token设置成请求头
            }else{
                config.headers.token = sessionStorage.token;  //将token设置成请求头
            }
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    }
);


async function getData(url,param){
    var ret = ''
    await axios.get(server+url, {params: param}).then(function (res) {
        ret = res.data
    })
    return ret
}

async function postData(url,param){
    var options = {  // 设置axios的参数
        url: server+url,
        data: param,
        method: 'post',
    }

    var ret = ''
    await axios(options).then(function (res) {
        ret = res
    })
    return ret
}

/**
 * async 异步处理
 * await 等待处理完成
 * @param url
 * @param param
 * @returns {Promise<string>}
 */
async function uploadFile(url,param){
    var options = {  // 设置axios的参数
        url: url,
        data: param,
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    }

    var ret = ''
    await axios(options).then(function (res) {
        ret = res.data
    })
    return ret
}

// export:导出变量、函数
export  {getData,postData,uploadFile}
